package com.example.service;

import java.lang.reflect.Method;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;

/*
 * 可被其它进程调用（无需重启）
 */
public class SystemServiceGetter implements SystemServiceMsgCode {
	private static final String TAG = "SystemServiceGetter";
	private static final String SERVICE_NAME = "System.Service";

	// dumpsys System.Service

	private static ISystemService iSystemService;
	private static Messenger mRemoteMessenger;
	private static SystemServiceGetter instance;

	static{
		Log.e(TAG, "load");
	}
	
	private SystemServiceGetter() throws Exception {
		Log.e(TAG, "new SystemServiceGetter()");
		mRemoteMessenger = new Messenger(getSystemService().getMessengerBinder());
	}

	/**
	 * 获得系统服务，可被其它进程调用(但无法调用系统级功能）
	 */
	public static ISystemService getSystemService() throws Exception {
		if (iSystemService == null) {
			Class<?> cServiceManager = Class
					.forName("android.os.ServiceManager");
			Method mGetService = cServiceManager.getDeclaredMethod(
					"getService", String.class);
			iSystemService = ISystemService.Stub
					.asInterface((IBinder) mGetService.invoke(null,
							SERVICE_NAME));
		}
		if (iSystemService == null) {
			throw new Exception("无法获得:" + SERVICE_NAME);
		}
		return iSystemService;
	}

	/**
	 * 只有通过这个才能调用系统级功能(其它app中也可以)
	 */
	public static SystemServiceGetter getPermissionService() {
		if (instance == null) {
			synchronized (SystemServiceGetter.class) {
				if (instance == null) {
					try {
						instance = new SystemServiceGetter();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return instance;
	}

	public void reboot() {
		sendEmptyMsg(REBOOT);
	}

	private boolean sendEmptyMsg(int what) {
		return sendMsg(what, 0, 0, null, null);
	}

	private boolean sendMsg(int what, Bundle data) {
		return sendMsg(what, 0, 0, null, data);
	}

	private boolean sendMsg(int what, int arg1, int arg2, Object obj, Bundle data) {
		Message msg = new Message();
		msg.what = what;
		msg.arg1 = arg1;
		msg.arg2 = arg2;
		if (obj != null){
			if(obj instanceof Parcelable){
				msg.obj = obj;
			}else{
				throw new IllegalArgumentException("obj must instanceof Parcelable!");
			}
		}
		if (data != null)
			msg.setData(data);

		try {
			mRemoteMessenger.send(msg);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void forceStopPackage(String pkg) {
		Bundle bundle = new Bundle();
		bundle.putString("p", pkg);
		sendMsg(FORCE_STOP_PACKAGE, bundle);
	}
	
	public void uninstall(String pkg) {
		Bundle bundle = new Bundle();
		bundle.putString("p", pkg);
		sendMsg(UNINSTALL, bundle);
	}
	
	public void install(String path) {
		Bundle bundle = new Bundle();
		bundle.putString("p", path);
		sendMsg(INSTALL, bundle);
	}
	
	public void clearData(String pkg) {
		Bundle bundle = new Bundle();
		bundle.putString("p", pkg);
		sendMsg(CLEAR_DATA, bundle);
	}
	
	public void clearCache(String pkg) {
		Bundle bundle = new Bundle();
		bundle.putString("p", pkg);
		sendMsg(CLEAR_CACHE, bundle);
	}
	
	/**
	 * 屏幕亮 {@link PowerManager#wakeUp(long)}
	 */
	public void deviceWakeUp(long time){
		Bundle bundle = new Bundle();
		bundle.putLong("p", time);
		sendMsg(DEVICE_WAKEUP, bundle);
	}
	/**
	 * 屏幕灭 {@link PowerManager#goToSleep(long)}
	 */
	public void deviceSleep(long time){
		Bundle bundle = new Bundle();
		bundle.putLong("p", time);
		sendMsg(DEVICE_SLEEP, bundle);
	}
	/**
	 * 杀进程 {@link android.os.Process#killProcess(int)}
	 */
	public void killProcess(int pid){
		sendMsg(KILLPROCESS, pid, 0, null, null);
	}
	
	public void deleteSystemDB(String key) {
		Bundle data = new Bundle();
		data.putString("p", key);
		sendMsg(DELETE_SYSTEM_DB, data);
	}
	
}
